//基于zepto.min、layer
var common = {
    //获取验证码  params:传的参数  o:按钮  cb回调函数
    getVerficatCode:function(params,o,cb){
        var _that = o,_params = params,count,_timer;
        function countDown(o){
            _that.attr('disabled',true).addClass('weui-btn_disabled').text('60秒后重发'),count = 60;
            _timer && clearInterval(_timer);
            _timer = setInterval(function(){
                count--;
                if (count > 0){
                    o.text(count + "秒后重发");
                }else {
                    clearInterval(_timer);
                    o.removeAttr('disabled style').removeClass("weui-btn_disabled").text("获取验证码");
                }
            }, 1000);
        }
        countDown(_that);
        $.post('/login/send_passcode',_params, function(data){
            if(data.status == 0){
                layer.open({content: '发送成功,请查收短信',skin: 'msg',time: 2});
            }else{
                _timer && clearInterval(_timer);
                _that.removeAttr('disabled style').removeClass("weui-btn_disabled").text("获取验证码");
                switch (data.status) {
                    case 11:
                        var _tpl = "<div class='weui-dialog__bd'>该手机号已经注册过，是否前去登录？</div>\
                            <div class='weui-dialog__ft'>\
                                <div class='weui-dialog__btn weui-dialog__btn_default'>取消</div>\
                                <div class='weui-dialog__btn weui-dialog__btn_primary'>前去登录</div>\
                            </div>";
                        layer.open({
                            shadeClose:false,
                            className: 'login_popuo',
                            shade:true,
                            content:_tpl,
                            success: function(elem){
                                $('.weui-dialog__btn_default',elem).on('click',function(e){
                                    layer.closeAll();
                                })
                                $('.weui-dialog__btn_primary',elem).on('click',function(e){
                                    layer.closeAll();
                                    cb && cb()
                                })
                            }
                        });
                        break;
                    case 12:
                    var _tpl = "<div class='weui-dialog__bd'>手机号还未注册过,是否立即注册?</div>\
                            <div class='weui-dialog__ft'>\
                                <div class='weui-dialog__btn weui-dialog__btn_default'>取消</div>\
                                <div class='weui-dialog__btn weui-dialog__btn_primary'>立即注册</div>\
                            </div>";
                        layer.open({
                            shadeClose:false,
                            className: 'login_popuo',
                            shade:true,
                            content: _tpl,
                            success: function(elem){
                                $('.weui-dialog__btn_default',elem).on('click',function(e){
                                    layer.closeAll();
                                })
                                $('.weui-dialog__btn_primary',elem).on('click',function(e){
                                    layer.closeAll();
                                    cb && cb()
                                })
                            }
                        });
                        break;
                    case 36:
                        var _tpl = "<div class='weui-dialog__bd'>账号不存在，请重新输入或注册新账号</div>\
                        <div class='weui-dialog__ft'>\
                            <div class='weui-dialog__btn weui-dialog__btn_default'>重新输入</div>\
                            <div class='weui-dialog__btn weui-dialog__btn_primary'>注册账号</div>\
                        </div>";
                        layer.open({
                            shadeClose:false,
                            className: 'login_popuo',
                            shade:true,
                            content: "",
                            success: function(elem){
                                $('.weui-dialog__btn_default',elem).on('click',function(e){
                                    layer.closeAll();
                                })
                                $('.weui-dialog__btn_primary',elem).on('click',function(e){
                                    layer.closeAll();
                                    cb && cb()
                                })
                            }
                        });
                        break;
                    case 130:
                        var captcha_guid = data.captcha_guid;
                        var captcha_url = data.captcha_url;
                        var _TplStr = "<h2>请在下方输入图形验证码</h2>\
                            <p class='port_error'></p>\
                            <div class='close_btn'><i class='icon iconfont icon-guanbianniu'></i></div>\
                            <div class='picture'>\
                                <div class='input-g'><input type='text' name='code' class='check_code' autofocus='true'/></div>\
                                <div class='pic-g'><img class='pic_code'/><span class='checkcode'>看不清换一张</span></div>\
                            </div>\
                        <button class='weui-btn weui-btn_mini verify_btn weui-btn_disabled' disabled='disabled'>确定</button>";
                        layer.open({
                            className: 'popuo-picture',
                            shadeClose: false,
                            content: _TplStr,
                            success: function(elem){
                                //获取图像验证码
                                function getCheck(){
                                    $('.pic_code',elem).attr('src',""); 
                                    captcha_guid = '';
                                    $.getJSON('/captcha_pair',{mobile:_params.mobile},function(o){
                                        if(o.status === 0){
                                            $('.pic_code',elem).attr('src',o.captcha_url); 
                                            captcha_guid = o.captcha_guid;
                                        }
                                    })
                                }
                                //获取图像验证码
                                $('.pic_code',elem).attr('src',captcha_url); 
                                //刷新图像验证码
                                $('.checkcode',elem).on('click',function(e){
                                    getCheck()
                                })
                                $("input[name='code']",elem).on('input', function(e) {
                                    if($(this).val().trim()){
                                        $('.verify_btn',elem).removeAttr('disabled').removeClass('weui-btn_disabled');
                                    }else{
                                        $('.verify_btn',elem).attr('disabled',true).addClass('weui-btn_disabled');
                                    }
                                })
                                //关闭图片验证码
                                $('.close_btn',elem).on('click',function(e){
                                    layer.closeAll()
                                })
                                //验证输入的验证码和给的验证码
                                $('.verify_btn',elem).on('click',function(e){
                                    $(this).attr('disabled',true).addClass('weui-btn_disabled').text('验证中...');
                                    var yparams = _params
                                    yparams.captcha_guid = captcha_guid
                                    yparams.captcha = $("input[name='code']",elem).val().trim();
                                    $.getJSON('/validate_captcha',yparams,function(o){
                                        if(o.status === 0){
                                            $('.port_error',elem).hide().text('');
                                            layer.closeAll();
                                            countDown(_that);
                                            $.post('/login/send_passcode',yparams, function(data){
                                                if(data.status === 0){
                                                    layer.open({content: '发送成功,请查收短信',skin: 'msg',time: 2});
                                                }else{
                                                    _timer && clearInterval(_timer);
                                                    _that.removeAttr('disabled style').removeClass("weui-btn_disabled").text("获取验证码");
                                                }
                                            })
                                        }else{
                                            $('.port_error',elem).show().text(o.error_info);
                                        }
                                        $('.verify_btn',elem).removeAttr('disabled').removeClass('weui-btn_disabled').text("确定");
                                    })
                                })
                            }
                        });
                        break;
                    default:
                        layer.open({content: data.error_info,skin: 'msg',time: 2});
                        break;
                }
            }
        })
    },
    //通用错误弹窗
    commonPopop:function(title){
        layer.open({
            time:2,
            shadeClose:false,
            className: 'common_popuo',
            shade:true,
            content: "<div class='label'><i class='icon iconfont icon-gantanhao'></i></div><p class='title'>"+title+"</p>"
        });
    },
    //通用加载Loading
    spinLoading:function(title){
        layer.open({
            shadeClose:false,
            shade: 'background-color: rgba(0,0,0,0)',
            className: 'spinloading_popuo',
            content: "<div class='spinLoading'><div class='loader'></div></div><p class='title'>"+title+"</p>"
        });
    }
}